using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using DynSim;
using DynSim.EntityFramework;
using DynSim.Basics;
using DynSim.Entities;

namespace Test2
{
    static class Program
    {
        static void Main(string[] args)
        {
            new Test2().Run();
        }
    }

    public class Test2 : Game
    {
        BaseEngine engine;
        FPSCalculator fps;

        public Test2()
        {
            engine = new BaseEngine(this);

            engine.antialisingPass = 4;

            engine.camera = new Camera(new Vector3(0), new Vector3(0, 0, 1), 1f, (float)engine.graphics.PreferredBackBufferWidth / (float)engine.graphics.PreferredBackBufferHeight, 0.1f, 10000f);
        }

        protected override void Initialize()
        {
            fps = new FPSCalculator(this);

            engine.entities.AddAndInitialize(new Clear(Color.Black, this, "clear"));
            engine.entities.AddAndInitialize(new BasicText("font", new Vector3(10, 10, 0), engine, this, "FPS: -", Color.White, "fps"));
            engine.entities.AddAndInitialize(new BasicModel(new Vector3(0, 0, 1), new Vector3(0), 3f, engine, "cube", this, "cube"));
            engine.entities.AddAndInitialize(new ControlsHandler(engine, "handler"));

            ControlsHandler handler = (ControlsHandler)engine.entities[3];

            Control left = new Control(Keys.Left, Buttons.DPadLeft);
            left.buttonPressedEventHandler += leftButtonPressed;
            handler.controls.Add(left);

            Control right = new Control(Keys.Right, Buttons.DPadRight);
            right.buttonPressedEventHandler += rightButtonPressed;
            handler.controls.Add(right);

            Control up = new Control(Keys.Up, Buttons.DPadUp);
            up.buttonPressedEventHandler += upButtonPressed;
            handler.controls.Add(up);

            Control down = new Control(Keys.Down, Buttons.DPadDown);
            down.buttonPressedEventHandler += downButtonPressed;
            handler.controls.Add(down);

            Control esc = new Control(Keys.Escape, Buttons.Back);
            esc.buttonPressedEventHandler += escButtonPressed;
            handler.controls.Add(esc);

            base.Initialize();
        }

        void leftButtonPressed(object sender, EventArgs e)
        {
            BasicModel model = (BasicModel)engine.entities[2];
            model.rotation = new Vector3(model.rotation.X, model.rotation.Y + 0.1f, 0);
        }

        void rightButtonPressed(object sender, EventArgs e)
        {
            BasicModel model = (BasicModel)engine.entities[2];
            model.rotation = new Vector3(model.rotation.X, model.rotation.Y - 0.11f, 0);
        }

        void upButtonPressed(object sender, EventArgs e)
        {
            BasicModel model = (BasicModel)engine.entities[2];
            model.rotation = new Vector3(model.rotation.X + 0.1f, model.rotation.Y, 0);
        }

        void downButtonPressed(object sender, EventArgs e)
        {
            BasicModel model = (BasicModel)engine.entities[2];
            model.rotation = new Vector3(model.rotation.X - 0.1f, model.rotation.Y, 0);
        }

        void escButtonPressed(object sender, EventArgs e)
        {
            this.Exit();
        }

        protected override void Update(GameTime gameTime)
        {
            BasicText text = (BasicText)engine.entities[1];
            text.text = "FPS: " + fps.frameRate;

            base.Update(gameTime);
        }
    }
}
